# -*- coding: utf-8 -*-
# 导入必要的库
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import BPNN
from sklearn import metrics
from sklearn.metrics import mean_absolute_error
from sklearn.metrics import mean_squared_error
import pickle
import os
import time
from sklearn import preprocessing
from dataset import dataset
from parser import args
from logger import *
# logger = logger1()

def mape1(actual, pred):
    actual, pred = np.array(actual), np.array(pred)
    return np.mean(np.abs((actual - pred) / actual)) * 100


def evaluate(num, bp1,logger, test_data, y_test, df2):
    load_model = bp1
    # _, test_data, _, _, y_test, df2 = dataset(logger)
    y_predict = load_model.predict(test_data)
    y_pre = np.array(y_predict)  # 列表转数组
    y_pre = y_pre.reshape(args.cut, 1)
    y_pre = y_pre[:, 0]
    # 画图 #展示在测试集上的表现
    # print("%%%%%%%%%%%%%%%")
    # print(len(y_pre))
    # print(type(y_pre))
    # print(len(y_test))
    # draw = pd.concat([pd.DataFrame(y_test), pd.DataFrame(y_pre)], axis=1)
    # draw.iloc[:, 0].plot(figsize=(12, 6))
    # draw.iloc[:, 1].plot(figsize=(12, 6))
    # plt.legend(('real', 'predict'), loc='upper right', fontsize='15')

    # 输出精度指标
    # print(type(y_pre))
    # print('测试集上的MAE/MSE')

    # mape = np.mean(np.abs((y_pre - y_test) / (y_test))) * 100
    # mape = np.mean(np.abs((y_pre - y_test) / (y_test))) * 100
    # print('=============mape==============')
    # print(mape, '%')
    # 画出真实数据和预测数据的对比曲线图
    # print("R2 = ", metrics.r2_score(y_test, y_pre))  # R2
    # print(y_test)

    # print((pd.DataFrame(y_test)))

    # 归一化逆变换
    # data_true1 = df2.iloc[-cut:]  # 原始的真实数据
    # print(data_true1)
    close_max = df2['target_col'].max()  # 收盘价的最大值
    close_min = df2['target_col'].min()  # 收盘价的最小值
    # print(close_min, close_max)
    data_true = pd.DataFrame(y_test)  # 归一化的真实数据
    true_nums = []
    preds_nums = []
    # print(type(y_test))
    for i in range(len(data_true)):
        # print(y_test[i])
        true_num = y_test[i][0] * (close_max - close_min) + close_min
        preds_num = y_pre[i] * (close_max - close_min) + close_min
        # print(true_num)
        true_nums.append(true_num)
        preds_nums.append(preds_num)
    # print(true_nums)
    # print(preds_nums)
    # mape = "mape", num, " = ", mape1(true_nums, preds_nums)
    # r2 = "R2 ", num, "= ", metrics.r2_score(true_nums, preds_nums)
    # print(mape)
    # logger.info("")
    logger.info(f"mae {num} = {mean_absolute_error(preds_nums, true_nums)}")
    logger.info(f"mse {num} = {mean_squared_error(preds_nums, true_nums)}")
    logger.info(f"mape {num} = {mape1(true_nums, preds_nums)}")
    # print(r2)  # R2
    logger.info(f'R2 {num} = {metrics.r2_score(true_nums, preds_nums)}')
    # 画图
    # plt.plot(true_nums)
    # plt.plot(preds_nums)
    # plt.legend(('real', 'predict'), fontsize='15')
    # plt.title("Test Data", fontsize='30')  # 添加标题
    # plt.show()
    return



